//
//  QKTextField.m
//  QKiOS
//
//  Created by syxiaqj on 15/6/28.
//  Copyright (c) 2015年 qingka. All rights reserved.
//

#import "QKTextField.h"

@interface QKTextField () <UITextFieldDelegate>

@property (nonatomic, strong) NSDictionary *placeholderAttributes;

@end

@implementation QKTextField

#pragma mark - Override

- (instancetype)initWithFrame:(CGRect)frame {
	self = [super initWithFrame:frame];
	if (self) {
		self.clearButtonMode = UITextFieldViewModeWhileEditing;
		self.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter;
		self.returnKeyType = UIReturnKeyDone;
	}
	return self;
}

- (void)drawPlaceholderInRect:(CGRect)rect {
	//重设placeholder的颜色
	
	CGSize size = [self.placeholder sizeWithAttributes:self.placeholderAttributes];
	
	CGRect drawRect = CGRectMake(0, (rect.size.height - size.height) / 2.0, size.width, size.height);
	
	[self.placeholder drawInRect:drawRect withAttributes:self.placeholderAttributes];
}

#pragma mark - UITextFieldDelegate

- (BOOL)textFieldShouldReturn:(UITextField *)textField {
	return [self resignFirstResponder];
}

#pragma mark - Pri Property

- (NSDictionary *)placeholderAttributes {
	if (!_placeholderAttributes) {
		_placeholderAttributes = @{NSForegroundColorAttributeName: COLOR_WITH_HEX_A(0x000000, 0.25),
								   NSFontAttributeName: self.font};
	}
	return _placeholderAttributes;
}


@end
